package com.atguigu.controller;


import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.service.UserInfoService;
import com.atguigu.util.MD5;
import com.atguigu.vo.LoginVo;
import com.atguigu.vo.RegisterVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/userInfo")
public class UserInfoController {
    @DubboReference
    private UserInfoService userInfoService;

    @RequestMapping("/logout")
    @ResponseBody
    public Result logout(HttpServletRequest request) {
        request.getSession().removeAttribute("USER");
        return Result.ok();
    }

    @RequestMapping("/login")
    @ResponseBody
    public Result login(@RequestBody LoginVo loginVo, HttpServletRequest request) {
//        从前端获取手机号和密码
        String phone = loginVo.getPhone();
        String password = loginVo.getPassword();
//        判断手机号和密码是否为空
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(password)) {
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
//        判断是否存在用户
        UserInfo userinfo = userInfoService.getByPhone(phone);
        if (null == userinfo) {
            return Result.build(null, ResultCodeEnum.ACCOUNT_ERROR);
        }
//        判断密码是否错误
        if (!MD5.encrypt(password).equals(userinfo.getPassword())) {
            return Result.build(null, ResultCodeEnum.PASSWORD_ERROR);
        }
//        判断用户是否被禁用
        if (userinfo.getStatus() == 0) {
            return Result.build(null, ResultCodeEnum.ACCOUNT_LOCK_ERROR);
        }
//      登录成功将用户userinfo存入session
        request.getSession().setAttribute("USER",userinfo);
//      响应电话号和名称
        Map<String, Object> map = new HashMap<>();
        map.put("phone", userinfo.getPhone());
        map.put("nickName", userinfo.getNickName());
        return Result.ok(map);
    }

    @RequestMapping("/register")
    @ResponseBody
    public Result register(@RequestBody RegisterVo registerVo,HttpServletRequest request) {
//        获取注册信息
        String nickName = registerVo.getNickName();
        String phone = registerVo.getPhone();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();
        //校验参数
        if(StringUtils.isEmpty(nickName) ||
                StringUtils.isEmpty(phone) ||
                StringUtils.isEmpty(password) ||
                StringUtils.isEmpty(code)) {
            return Result.build(null, ResultCodeEnum.PARAM_ERROR);
        }
        //验证码
        String currentCode = (String)request.getSession().getAttribute("CODE");
        if(!code.equals(currentCode)) {
            return Result.build(null, ResultCodeEnum.CODE_ERROR);
        }
//      判断手机号是否已注册
        UserInfo userInfo = userInfoService.getByPhone(phone);
        if(null != userInfo) {
            return Result.build(null, ResultCodeEnum.PHONE_REGISTER_ERROR);
        }

//      获取注册信息存入数据库
        userInfo = new UserInfo();
        userInfo.setNickName(nickName);
        userInfo.setPhone(phone);
        userInfo.setPassword(MD5.encrypt(password));
        userInfo.setStatus(1);
        userInfoService.insert(userInfo);
        return Result.ok();
    }

    @RequestMapping("/sendCode/{phone}")
    @ResponseBody
    public Result sendCode(@PathVariable("phone") String phone, HttpServletRequest request) {
//        调用第三方服务，向手机发送验证码
        String code = "1111";
//        将验证码存入session
        HttpSession session = request.getSession();
        session.setAttribute("CODE",code);
        return Result.ok(code);

    }
}
